
<template>
  <div id="deptadmin">
    <el-button type="primary" style="margin-bottom: 10px;" @click="openUser()" icon="Plus">添加</el-button>
    <div class="tablecon" >
      <el-table id="out" border :data="RoleList" height="700">
        <el-table-column align="center" type="index" label="序号" width="60" />
        <el-table-column prop="code" label="角色" width="250" />
        <el-table-column prop="name" label="名称" width="200" />
        <el-table-column prop="remark" label="说明" width="300">
          <template #default="scope">
            <span class="text-limit" :title="scope.row.remark">{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="">
          <template #default="scope">
            <div v-if="f_IsNoEdit(scope.row.code)">
              <el-button size="default" type="success" @click="updateDept(scope.row)" icon="Edit">修改</el-button>
              <el-button size="default" type="warning" @click="removeDept(scope.row)" icon="Delete">删除</el-button>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="" label="" /> -->
      </el-table>
    </div>
    <!-- <pagination style="margin-top: 10px;" @sendsize="getsize" @sendpage="getpage" :page="page"></pagination> -->
    <!-- 用户弹框 -->
    <el-dialog v-model="DeptDlg" title="角色管理" width="600" align-center>
      <div>
        <el-form :model="deptForm" label-width="100" class="demo-form-inline" style="margin: 10px;">
          <el-form-item label="角色代码：">
            <el-input v-model="deptForm.code" style="width: 300px;" />
          </el-form-item>
          <el-form-item label="角色名称：">
            <el-input v-model="deptForm.name" style="width: 300px;" />
          </el-form-item>
          <el-form-item label="角色描述：">
            <el-input type="textarea" v-model="deptForm.remark" style="width: 400px;" rows="4" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addUser()" icon="Select">
            确定
          </el-button>
          <el-button @click="DeptDlg=false" icon="Close">取消</el-button>

        </div>
      </template>
    </el-dialog>
    
  </div>
</template>

<script setup>
import axios from 'axios';
import { onMounted, ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
import $sys from "../../api/sys";
import pagination from '../../components/pagination.vue';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';

const deptList = ref([]);
const roleDlg = ref(false);
const page = reactive({
  pagenum: 1,
  pagesize: 10,
  total: 0
})
onMounted(() => {
  getRoleList();
})

function cancelRoleChoice() {
  roleListDlg.value = false;
  userroleForm.role_code = '';
}

function f_IsNoEdit(name) {
  if (name == 'common' || name == 'administrator')
    return false;
  else
    return true;
}

function DelRole(e) {
  ElMessageBox.confirm(
    '确定删除该角色?',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    console.log(e);
    let obj = {};
    obj.role_code = e.code;
    obj.user_id = userID.value;
    $sys.DelUserRole(obj).then(res => {
      if (res.code == 200) {
        ElMessage({
          message: '删除成功',
          type: 'success'
        })
        let obj = {};
        obj.id = userID.value;
        watchUserRole(obj);
      }
    })
  })
}
const roleListDlg = ref(false);
const userroleForm = reactive({});
function addRole() {
  if (userroleForm.role_code == '' || !userroleForm.role_code) {
    ElMessage({
      message: '请选择角色',
      type: 'warning',
    })
  } else {
    userroleForm.user_id = userID.value;
    $sys.addUserRole(userroleForm).then(res => {
      if (res.code == 200) {
        ElMessage({
          message: '添加成功',
          type: 'success'
        })
        cancelRoleChoice();
        let obj = {};
        obj.id = userID.value;
        watchUserRole(obj);
      }
    })
  }
}
function openRoleDlg() {
  roleListDlg.value = true;
}
const RoleList = ref([]);
function getRoleList() {
  $sys.getRoleList().then(res => {
    if (res.code == 200) {
      RoleList.value = res.data;
      console.log(111, RoleList.value);
    }
  })
}
const userroleList = ref([]);
const userID = ref('');
function watchUserRole(e) {
  userID.value = e.id;
  $sys.getRoleListByUser(e.id).then(res => {
    if (res.code == 200) {
      userroleList.value = res.data;
      roleDlg.value = true;
    }
  })
}

function f_msg(msg) {
  ElMessage(msg);
}

function addUser() {
  if (deptForm.code == ""||deptForm.name == "") {
    f_msg("角色编码和名称不能为空！");
    return;
  }
  
  if (deptForm.isupdate) {
    //表示修改
    
    var data = {
      "id":deptForm.id,
      "name": deptForm.name,
      "code": deptForm.code,
      "remark": deptForm.remark
    };

    $sys.upMyUserRole(data).then(res => {
      if (res.code == 200) {
        ElMessage({
          message: '操作完成！',
          type: 'success'
        })
        DeptDlg.value=false;
        getRoleList();
      }
    })
  }
  else {
    //表示新建
    var data = {
      "name": deptForm.name,
      "code": deptForm.code,
      "remark": deptForm.remark
    };
    $sys.addMyUserRole(data).then(res => {
      if (res.code == 200) {
        ElMessage({
          message: '操作完成！',
          type: 'success'
        })
        DeptDlg.value=false;
        getRoleList();
      }
    })
  }
}
const DeptDlg = ref(false);
const deptForm = reactive({
  username: '',
  password: '',
  id: '',
  name: '',
  code: '',
  remark: '',
  isupdate: false,
});
function cancle() {
  initdeptForm();
  getDeptList();
  DeptDlg.value = false;
}
function initdeptForm() {
  deptForm.name = '';
  deptForm.code = '';
  deptForm.remark = '';
  deptForm.parent_code = 'top';
  deptForm.isupdate = false;
}
function openUser() {
  initdeptForm();
  DeptDlg.value = true;
}


function updateDept(e) {

  DeptDlg.value = true;
  deptForm.id = e.id;
  deptForm.name = e.name;
  deptForm.code = e.code;
  deptForm.remark = e.remark;
  deptForm.isupdate = true;
}


function removeDept(e) {
  ElMessageBox.confirm(
    '是否确定？',
    '警告',
    {
      cancelButtonText: '取消',
      confirmButtonText: '确定',
      type: 'warning',
    }
  ).then(() => {
    $sys.delMyUserRole(e.id).then(res => {
      if (res.code == 200) {
        ElMessage({
          message: '操作完成！',
          type: 'success'
        })
        DeptDlg.value=false;
        getRoleList();
      }
    })
  }).catch(() => {

  })

}

</script>
<style scoped>
:deep(*) {
  font-size: medium;
}
</style>
<style scoped>
:deep(*) {
  font-size: medium;
}

.el-form-item__label {
  color: deepskyblue;
}

#deptadmin {
  background-color: #2F4052;
  width: 100%;
  padding: 0 10px;
  height: 780px;
  padding-top: 10px;
}

.tablecon {
  overflow-y: auto;
}

.tablecon::-webkit-scrollbar {
  width: 5px;
}

.tablecon::-webkit-scrollbar-thumb {
  background-color: #ccc;
  /* 设置滚动条的颜色 */
  border-radius: 6px;
  /* 设置滚动条的圆角 */
}

.tablecon::-webkit-scrollbar-track {
  background-color: #888;
  border-radius: 6px;
  /* 设置滚动条的轨道颜色 */
}

:deep(.el-dialog) {
  background-color: #2F4052 !important;
}

:deep(.el-dialog__title),
:deep(.el-dialog__body) {
  color: deepskyblue !important;
}
</style>

<style lang='less'>
#deptadmin {
  .el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell {
    background: none;
  }

  .el-table__inner-wrapper {
    &::before {
      display: none;
    }
  }

  // 头部样式
  .el-table__header-wrapper th {
    background-color: #2F4052;
    color: #fff;
  }

  // 内容行样色
  .el-table__row {
    background-color: #2F4052;
    color: #B2C9EE;
  }

  // 内容行边框
  .el-table__cell {
    border: 1 !important;
    border-color: #222e3b;
  }

  .el-table--border .el-table__inner-wrapper:after,
  .el-table--border:after,
  .el-table__border-left-patch {
    background-color: #222e3b;
  }

  //自定义斑马线和鼠标移入样式
  .el-table--enable-row-hover .el-table__body tr:hover:nth-child(even)>td {
    background-color: #263343 !important;
  }

  .el-table--enable-row-hover .el-table__body tr:hover:nth-child(odd)>td {
    background-color: #2F4052 !important;
  }

  .el-table__body tr:nth-child(even) {
    background-color: #263343;
    /* 偶数行（斑马线）的默认背景色 */
  }

  .el-table__empty-block {
    background-color: #233741;
  }

  .el-form-item__label {
    color: deepskyblue;
  }
}

.text-limit {
  white-space: nowrap;
  /* 保证文本在一行内显示 */
  overflow: hidden;
  /* 隐藏超出容器的文本 */
  text-overflow: ellipsis;
  /* 使用省略符号表示文本被截断 */
  width: 400px;
  /* 设置容器宽度 */
}
.el-scrollbar__wrap--hidden-default {
    background-color:#2F4052;
}
</style>

